//
//  UserAccount.swift
//  weibo
//
//  Created by 王、小胜 on 16/7/26.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit


/// 用户账户模型
class UserAccount: NSObject,NSCoding {
    
    ///用于调试access——token，获取授权后的access token
    var access_token: String?
    
    ///access_token的生命周期，单位秒
    ///一旦从服务器获得过期时间，立刻计算准确日期
    var expires_in: NSTimeInterval = 0 {
        didSet {
            //计算过期日期
           expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    ///用户昵称
    var screen_name:String?
    
    ///用户头像地址
    var avatar_large: String?
    
    ///过期日期
    var expiresDate: NSDate?
    
    ///当前用户授权的UID
    var uid: String?
    
    init(dict: [String: AnyObject] ) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        let keys = ["access_token","expires_in","expiresDate","uid","screen_name","avatar_large"]
        
        return dictionaryWithValuesForKeys(keys).description
        
    }
    
        //MARK: - 归档解档
    
    ///归档- 把当前对象保存到磁盘前编码成二进制数据
    
    func encodeWithCoder(aCoder: NSCoder){
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    ///解档： 从磁盘加载二进制文件转换成对象调用
    
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }

}
